/*
 * Copyright 2010 Christoph Widulle (christoph.widulle@googlemail.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the
 * License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS"
 * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */


package org.amarena2d.examples.scenes.invader;

import com.android1.amarena2d.commons.Callback;
import com.android1.amarena2d.commons.collections.FixedSizeQueuePool;
import com.android1.amarena2d.commons.event.Event;
import com.android1.amarena2d.commons.event.EventHandler;
import com.android1.amarena2d.nodes.particle.Emitter;
import com.android1.amarena2d.nodes.particle.ParticleSystem;

public class SpecialEffects extends ParticleSystem {


    private final FixedSizeQueuePool<Emitter> explosions;

    public SpecialEffects() {
        super(engine.getTextureManager().getTexture("invader/particle2.png"));

        engine.getEventBus().register(Events.INVADER_DESTROYED, new EventHandler() {
            @Override
            public void onEvent(Event event) {
                fireExplosion(event.getArg1(), event.getArg2());
            }
        });

        explosions = new FixedSizeQueuePool<Emitter>(5) {
            @Override
            protected Emitter newElement() {
                Emitter emitter = createEmitter(new ShipExplosionEffect(), 0, 0);
                emitter.setOnFinishCallback(onStopCallback);
                return emitter;
            }
        };
    }

    public void fireExplosion(float x, float y) {
        Emitter e = explosions.acquire();
        if (e != null) {
            e.setXY(x, y);
            e.play();
        }
    }


    public final Callback<Emitter> onStopCallback = new Callback<Emitter>() {
        @Override
        public void on(Emitter element) {
            explosions.release(element);
        }
    };
}
